﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace DVK.SP2007.UniversalClient
{
    /// <summary>
    /// Helper methods for managing some activities with Xml.
    /// </summary>
    public class XmlHelper
    {
        /// <summary>
        /// Creates the element.
        /// </summary>
        /// <param name="elementName">Name of the element.</param>
        /// <param name="elementValue">The element value.</param>
        /// <returns></returns>
        public static XElement CreateElement(XName elementName, string elementValue)
        {
            if (string.IsNullOrEmpty(elementValue))
                return null;

            return new XElement(elementName, elementValue);
        }

        /// <summary>
        /// Get element safely.
        /// </summary>
        /// <param name="currentElement">The current element.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string SafeGetElement(XElement currentElement, XName name)
        {
            XElement subElement = currentElement.Element(name);
            if (subElement != null) return subElement.Value;
            return null;
        }

        /// <summary>
        /// Date in the format as in ISO8601.
        /// <remarks>
        /// DateTime format information from url: http://www.w3.org/TR/NOTE-datetime
        /// Pattern: yyyy-MM-dd
        /// </remarks>
        /// </summary>
        /// <param name="dateTime">some date</param>
        /// <returns></returns>
        public static string GetISO8601Date(DateTime? dateTime)
        {
            //yyyy-MM-ddTHH:mm:ssZ
            if (dateTime.HasValue)
                return dateTime.Value.ToString("yyyy-MM-dd");

            return string.Empty;
        }

        /// <summary>
        /// DateTime in the format as in ISO8601.
        /// <remarks>
        /// DateTime format information from url: http://www.w3.org/TR/NOTE-datetime
        /// Pattern: yyyy-MM-ddTHH:mm:ssZ
        /// DateTime.ToString("s") is used.
        /// </remarks>
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string GetISO8601DateTime(DateTime? dateTime)
        {
            if (dateTime.HasValue)
                return dateTime.Value.ToString("s");   
                
            return string.Empty;
        }

        /// <summary>
        /// Time in the format as in ISO8601.
        /// <remarks>
        /// DateTime format information from url: http://www.w3.org/TR/NOTE-datetime
        /// Pattern: HH:mm:ss
        /// </remarks>
        /// </summary>
        /// <param name="dateTime">The date time.</param>
        /// <returns></returns>
        public static string GetISO8601Time(DateTime? dateTime)
        {
            if (dateTime.HasValue)
                return dateTime.Value.ToString("HH:mm:ss");
            return string.Empty;
        }
    }
}
